package fr.esiag.isidis.pdsTeam.UDP;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

	public class RawInformationSender {
		public int getPort() {
			return port;
		}

		public void setPort(int port) {
			this.port = port;
		}

		public String getIp() {
			return ip;
		}

		public void setIp(String ip) {
			this.ip = ip;
		}

		private static RawInformationSender instance=null;
		private int port;
		private String ip;
			
		public static RawInformationSender getInstance() throws UnknownHostException {
			if(instance==null) instance=new RawInformationSender();
			return instance;
		}

		public RawInformationSender() throws UnknownHostException {
			 XmlBeanFactory factory=new XmlBeanFactory(new ClassPathResource("UDP.xml"));
             IpPort adresse=factory.getBean("adresse",IpPort.class);
            
             ip=adresse.getIp();
             port=adresse.getPort();
		}

		public void sendTram(byte[]tram){
			try {
				byte[] papier = tram;
				InetAddress iad=InetAddress.getByName(ip);
				DatagramPacket lettre = new DatagramPacket(papier,papier.length,iad,port);
				DatagramSocket bal = new DatagramSocket();
				bal.send(lettre);
			} catch (Exception e){
				System.out.println("afficher erreur" + e.getMessage());
			}		
		}
}


